/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   PolicyStandardsServiceImpl.java for com.pengtu.gsj.service.impl.check.jsbz.preventMaintain    </p>
 * 
 * <p>最后修改 : $: 2017年8月9日-下午4:32:23 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jsbz.preventMaintain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.check.jsbz.preventMaintain.PolicyStandardsDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jsbz.PolicyCriterion;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jsbz.preventMaintain.PolicyStandardsService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * PolicyStandardsServiceImpl
 * 
 * 2017年8月9日 下午4:32:23
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class PolicyStandardsServiceImpl implements PolicyStandardsService {
	
	@Autowired
	private PolicyStandardsDao policyStandardsDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	@Override
	public List<PolicyCriterion> getPolicyCriterionList(QueryFilter queryFilter){
		return policyStandardsDao.find(queryFilter);
	}
	
	@Override
	public Map<String, Object> getPolicyCriterionMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<PolicyCriterion> trafficList = new ArrayList<PolicyCriterion>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//取得预防性养护意见文件
		List<Attachment> yfyhList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "zcbz_yfyh");
		//取得预防性养护技术文件
		List<Attachment> yfjsList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "zcbz_yhjs");
		double total =0; 		//检查得分
		double percentage =0; 	//系统完善度
		double tableFull  = 0;	//满分
		
		if (yfyhList.size() > 0) {
			total = MathsUtils.sumDouble(total, yearFull);
			map.put("yearFull", yearFull);
			map.put("yfyhList", yfyhList);
		}
		if (yfjsList.size() > 0) {
			double temp = 0;
			if (yfjsList.size() * yearPart > poitMark) {
				total = MathsUtils.sumDouble(total, poitMark);
				temp = MathsUtils.sumDouble(temp, poitMark);
			} else {
				total = MathsUtils.sumDouble(total, MathsUtils.mulDouble(yfjsList.size(), yearPart));
				temp = MathsUtils.sumDouble(temp, MathsUtils.mulDouble(yfjsList.size(), yearPart));
			}
			map.put("total",temp);
			map.put("yfjsList", yfjsList);
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		tableFull = MathsUtils.sumDouble(yearFull, poitMark);
		tableFull =	MathsUtils.formatDouble(tableFull,2);
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		map.put("percent", percentage);
			
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("overView", trafficList);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
